package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.TrainingActivity;
import ModelLayer.ActivityDetails;

public class DBActivityDetails implements DBActivityDetailsIF
{
	private Connection con;
    
    public DBActivityDetails()
    {
    	con = DBConnection.getInstance().getDBcon();
    }
	
    public ArrayList<ActivityDetails> getAllActivityDetails(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public ActivityDetails searchActivityDetailsID(int activityDetailsID, boolean retriveAssociation)
    {   
    	String wClause = "ActivityDetailsID = " + activityDetailsID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    private ArrayList<ActivityDetails> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<ActivityDetails> activityDetailsList = new ArrayList<ActivityDetails>();	
		
		String query = buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				ActivityDetails adObj = new ActivityDetails();
				adObj = buildActivityDetails(results);	
				activityDetailsList.add(adObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return activityDetailsList;
	}
	
	private ActivityDetails singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ActivityDetails adObj = new ActivityDetails();
	    String query = buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			adObj = buildActivityDetails(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return adObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM ActivityDetails a, TrainingActivity t";
	    query += " WHERE a.TrainingActivityID = t.TrainingActivityID";
		if (wClause.length()>0)
			query=query+" AND " + wClause;
		return query;
	}

	private ActivityDetails buildActivityDetails(ResultSet results)
      {   
		ActivityDetails adObj = new ActivityDetails();
          try
          {
                adObj.setActivityDetailsID(results.getInt("ActivityDetailsID"));
                adObj.setGoal(results.getString("Goal"));
                adObj.setRequirements(results.getString("Requirements"));
                adObj.setTrainingProcedure(results.getString("TrainingProcedure"));
                adObj.setPromptSuggestions(results.getString("PromptSuggestions"));
                adObj.setMaterial(results.getString("Material"));
                adObj.setTrainingMethod(results.getString("TrainingMethod"));
                adObj.setGeneralization(results.getString("Generalization"));
                adObj.setTrainingActivity(new TrainingActivity(results.getString("Name")));
          }
         catch(Exception e)
         {
             System.out.println("error in building the ActivityDetails object");
         }
         return adObj;
      }
}
